package com.yuexunit.operationman.nbsy.module.Placement.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.InterventionalOperation.Bean.DrugBean;
import com.yuexunit.operationman.nbsy.module.InterventionalOperation.Bean.DrugListBean;
import com.yuexunit.operationman.nbsy.module.InterventionalOperation.model.impl.DrugModel;
import com.yuexunit.operationman.nbsy.module.InterventionalOperation.presenter.IDrugPresenter;
import com.yuexunit.operationman.nbsy.module.InterventionalOperation.presenter.impl.DrugPresenter;
import com.yuexunit.operationman.nbsy.module.InterventionalOperation.ui.view.IDrugView;
import com.yuexunit.operationman.nbsy.widget.CustomDatePicker;
import com.yuexunit.operationman.nbsy.widget.FixedSwipeListView;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/6 0006.
 */

public class Act_Placement_Device extends BaseKJActivity implements IDrugView {

    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;
    @Bind(R.id.tv_submit)
    TextView tvSubmit;
    @Bind(R.id.listview)
    FixedSwipeListView listview;
    @Bind(R.id.iv_drug_list)
    LinearLayout ivDrugList;
    @Bind(R.id.reselect)
    TextView reselect;
    @Bind(R.id.drug_name)
    EditText drugName;
    @Bind(R.id.iv_drug_input)
    LinearLayout ivDrugInput;

    Act_Placement_Device.MyAdapter adapter;
    ArrayList<DrugListBean.ObjectEntity.ListEntity> datas = new ArrayList<>();
    DrugBean drug = new DrugBean();;
    int type,position=-1;

    LoadingDialog loadingDialog;
    IDrugPresenter presenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_placement_device);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        setTitle();
        presenter = new DrugPresenter(this,new DrugModel());
        loadingDialog = new LoadingDialog(this);

        tvSubmit.setText("取消");
        type = getIntent().getExtras().getInt("type");
        if(type == 2){
            position = getIntent().getExtras().getInt("position");
            drug = (DrugBean) getIntent().getExtras().getSerializable("drug");
            ivDrugList.setVisibility(View.GONE);
            ivDrugInput.setVisibility(View.VISIBLE);
            reselect.setVisibility(View.GONE);
            tvSubmit.setText("确认");

            drugName.setText(drug.getName());
        }


        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ivDrugList.setVisibility(View.GONE);
                ivDrugInput.setVisibility(View.VISIBLE);
                tvSubmit.setText("确认");
                drugName.setText(datas.get(i).getMedicineName());
            }
        });

    }

    @Override
    public void onResume(){
        super.onResume();
        if(datas.size()  == 0) {
            Map<String, String> params = new HashMap<>();
            params.put("pageNo", "1");
            params.put("pageSize", "9999");
            params.put("medicineType", "4");
            presenter.getDrugDate(params);
        }
    }

    private void setTitle() {
        tvPatientName.setText(AppConfig.PLACEMENTBEAN.getObject().getPatientName());
        if (AppConfig.PLACEMENTBEAN.getObject().getSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.PLACEMENTBEAN.getObject().getSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.PLACEMENTBEAN.getObject().getHospitalNo());
        tvPatientDisease.setText(PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_DISEASE));
    }


    @OnClick({R.id.titlebar_docadvice_img_back, R.id.tv_submit, R.id.input_bt, R.id.reselect
    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.tv_submit:
                if(tvSubmit.getText().toString().equals("确认")){
                    if(drugName.getText().toString().isEmpty()){
                        ViewInject.toast("请输入固定装置名");
                        break;
                    }
                    drug.setName(drugName.getText().toString());
//                    drug.setStartTime(startTime.getText().toString());
//                    drug.setEndTime(endTime.getText().toString());
                    Bundle bundle = new Bundle();
                    Intent intent = new Intent();
                    bundle.putInt("position",position);
                    bundle.putSerializable("drug", drug);
                    intent.putExtras(bundle);
                    setResult(20000, intent);
                }
                finish();
                break;
            case R.id.input_bt:
                ivDrugList.setVisibility(View.GONE);
                ivDrugInput.setVisibility(View.VISIBLE);
                tvSubmit.setText("确认");
                drugName.setText("");
                break;
            case R.id.reselect:
                ivDrugList.setVisibility(View.VISIBLE);
                ivDrugInput.setVisibility(View.GONE);
                tvSubmit.setText("取消");
                break;
//            case R.id.iv_start:
//                startTxt.setText("开始时间：");
//                initDatePicker(startTime);
//                break;
//            case R.id.iv_end:
//                endTxt.setText("结束时间：");
//                initDatePicker(endTime);
//                break;
        }
    }

    private void initDatePicker(final TextView view) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        Date date1 = null;
        if(view.getText().toString().isEmpty()){
            date1 = new Date();
        }else{
            try {
                date1 = sdf.parse(view.getText().toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        String now = sdf.format(date1);

        CustomDatePicker customDatePicker1 = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间
                view.setText(time);
            }
        }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker1.showSpecificTime(true); // 显示时和分
        customDatePicker1.setIsLoop(true); // 循环滚动
        customDatePicker1.show(now);
    }

    @Override
    public void getDrugInfo(DrugListBean drugListBean) {
        if(drugListBean.getCode() == 200)
        {
            datas = drugListBean.getObject().getList();
            adapter = new Act_Placement_Device.MyAdapter(datas);
            listview.setAdapter(adapter);
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }

    class MyAdapter extends BaseAdapter {

        protected LayoutInflater mInflater;
        ArrayList<DrugListBean.ObjectEntity.ListEntity> datas;

        public MyAdapter(ArrayList<DrugListBean.ObjectEntity.ListEntity> datas) {
            mInflater = (LayoutInflater) aty.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas == null ? 0 : datas.size();
        }

        @Override
        public Object getItem(int i) {
            return datas.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Act_Placement_Device.MyAdapter.ViewHolder viewHolder;
            if (view == null) {
                view = this.mInflater.inflate(R.layout.item_txt, viewGroup, false);
                viewHolder = new Act_Placement_Device.MyAdapter.ViewHolder(view);
                view.setTag(viewHolder);
            }else {
                viewHolder = (Act_Placement_Device.MyAdapter.ViewHolder) view.getTag();
            }
            viewHolder.txtName.setText(datas.get(i).getMedicineName());
            return view;
        }

        class ViewHolder {
            @Bind(R.id.txt_name)
            TextView txtName;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
